<?php
/*class MyTestClass{
	
	public function __toString()
	{
		return get_class($this);
	}
	
}
$obj = new MyTestClass();
echo $obj;
die;*/
$pathToXmlFile = 'Db/users.xml';

// Load an XML string
$xmlStr = file_get_contents($pathToXmlFile);
//var_dump($xmlStr);die;
$users = simplexml_load_string($xmlStr);
//var_dump($users);die;

// Load an XML file
$users = simplexml_load_file($pathToXmlFile);
//var_dump($users);die;

#==================OOP=========================
try{
	// Load an XML string
	$users = new SimpleXMLElement($xmlStr);
	//var_dump($users);die;
	
	// Load an XML file
	//$data, $options = null(Optionally used to specify additional Libxml parameters.), $data_is_url
	$users = new SimpleXMLElement($pathToXmlFile, null, true);
	//var_dump($users);;die;
	//Accessing Children and Attributes
	
	foreach ($users->user as $user){
		//var_dump($user->getName());die;
		//var_dump($user);die;
	/*	echo $user['id'] . PHP_EOL;
		echo $user['title'] . PHP_EOL;
		echo $user->firstName . PHP_EOL;
		echo $user->lastName . PHP_EOL;
		echo $user->email . PHP_EOL. PHP_EOL;*/
	}
	//die;
	
	//חיסרון משמעות שזה HC 
	
	foreach ($users->children() as $child){
		//var_dump($child);die;
		//echo $child->getName() . ':'.PHP_EOL;
		
		// Get attributes of this element
		//var_dump($child->attributes());die;
		foreach ($child->attributes() as $attr){
			//var_dump(get_class($attr));die;
			//var_dump(get_class_methods(get_class($attr)));die;
			//echo ' ' . $attr->getName() . ': ' . $attr. PHP_EOL;
		}
		//echo '--------------';
		// Get children
		foreach ($child->children() as $subChild){
			//echo ' ' .$subChild->getName() . ': ' . $subChild->__toString() . PHP_EOL;	
			foreach ($subChild->attributes() as $attr){
				//var_dump(get_class($attr));die;
				//var_dump(get_class_methods(get_class($attr)));die;
				//echo $subChild->getName().' attrs:     ' . $attr->getName() . ': ' . $attr. PHP_EOL;
			}
		}
	}
	//die;
	//XPath Queries like sql
	// Search the root element
	//http://www.w3schools.com/xpath/xpath_syntax.asp
	
	$results = $users->xpath('/users/user/email');
	//var_dump($results);die;
	foreach ($results as $email){
		//echo $email. PHP_EOL;
	}
	//die;
	//$results = $users->xpath('/users/user[@id="1"]/email');
//	$results = $users->xpath('/users/user[email="user3@user3.com"]/firstName');
	//var_dump($results);die;
	//var_dump($users);die;
	$results = $users->user[1]->xpath('email');
	//var_dump($users->user[1]->email);die;
//	var_dump($results);die;
	foreach ($results as $email){
		//var_dump($email->__toString());die;
	}
	
	//Modifying XML Documents
	//$user = $users->addChild('user');
	//$user->addAttribute('id', 4);
	/*$user->addAttribute('title', 'Modifying XML Documents');
	$user->addChild('firstName', 'user');
	$user->addChild('lastName', 4);
	$user->addChild('email', 'user4@user4.com');
	var_dump($users->asXML($pathToXmlFile));die;*/
	//delete item
	//This only removes child elements and their attributes
	//$users->user[0] = null;
	//header('Content-type: text/xml');
	//echo $users->asXML();
	
	//save to file
	//var_dump($users->asXML($pathToXmlFile));
	/*$dom = new DOMDocument();
	$dom->preserveWhiteSpace = false;
	$dom->formatOutput = true;
	$dom->loadXML($users->asXML());
	$dom->save($pathToXmlFile);*/
}catch(Exception $e){
	var_dump($e->getMessage());
}

//XPath Queries like sql
// Search the root element